﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using AUV5.Model;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     Bulletin业务逻辑实现
    /// 作者：     LI
    /// 日期：     12/21/2011
    /// </summary>
    public class BulletinBLL
    {
        private AUV5.DAL.Interface.IBulletinDao dao = null;

        private BulletinBLL()
        {
            dao = AUV5.DAL.Dao.BulletinSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static BulletinBLL instance = null;
        private static object syncRoot = new object();
        public static BulletinBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new BulletinBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public Bulletin GetDetail(System.String bulletinid)
        {
            try
            {
                return dao.GetDetail(bulletinid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<Bulletin> GetList()
        {
            try
            {
                IList<Bulletin> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<Bulletin> GetListByParams(object TopCounts, Bulletin bulletin)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<Bulletin> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="bulletin"></param>
        /// <returns></returns>
        public object Add(Bulletin bulletin)
        {
            try
            {
                return dao.Add(bulletin);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="bulletin"></param>
        /// <returns></returns>
        public int Update(Bulletin bulletin)
        {
            try
            {
                return dao.Update(bulletin);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="BulletinID"></param>
        /// <returns></returns>
        public int Delete(System.String bulletinid)
        {
            try
            {
                return dao.Delete(bulletinid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #region 添加公告 Via Vinlon

        /// <summary>
        /// 添加公告
        /// </summary>
        /// <param name="title">公告标题</param>
        /// <param name="content">公告内容</param>
        /// <param name="userid">用户编号为空代表发送给所有人</param>
        /// <param name="showalert">是否提示用户</param>
        public void AddBulletin(string title, string content, string userid, bool showalert, int status, string RedirectID)
        {
            try
            {
                Bulletin bulletin = new Bulletin();
                bulletin.BulletinID = Guid.NewGuid().ToString();
                bulletin.Title = title;
                bulletin.BulletinContent = content;
                bulletin.UserID = userid;

                bulletin.ShowAlert = showalert;
                string url = RedirectID;
                //if (status == (int)Common.Enum.bulletinStatus.BeBuyer)
                //{
                //    url = "/Pages/ShoppingDetail.aspx?id=" + RedirectID;
                //}
                //else if(status==(int)Common.Enum.bulletinStatus.BeSeller)
                //{
                //    url = "/Pages/OrderDetail.aspx?id=" + RedirectID;
                //}

                bulletin.RedirectID = url;

                bulletin.StatusID = status;
                bulletin.IsRead = false;
                bulletin.IsDel = (int)Common.Enum.auvDel.NotDel;
                bulletin.CreateDate = DateTime.Now;
                bulletin.UpdateDate = DateTime.Now;
                bulletin.BeginDate = DateTime.Now;
                bulletin.EndDate = DateTime.Now;

                Add(bulletin);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 获取所有未读公告 Via Vinlon

        /// <summary>
        /// 获取所有未读公告
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<Bulletin> GetUnReadBulletin(string userid)
        {
            try
            {
                //发送给指定用户的未读信息
                Hashtable ht = new Hashtable();
                ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);
                ht.Add("isRead", false);
                ht.Add("userID", userid);
                return dao.GetListByParams(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有需要提示的未读公告
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<Bulletin> GetAlertBulletin(string userid)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);
                ht.Add("isRead", false);
                ht.Add("userID", userid);
                ht.Add("showAlert", true);

                return dao.GetListByParams(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 读取分页的公告列表

        /// <summary>
        /// 读取的同时是否将未读公告设为已读
        /// </summary>
        /// <param name="read"></param>
        /// <returns></returns>
        public IList<Bulletin> GetPagedList(string userid, int pagesize, int pageindex, ref int recordcount)
        {
            try
            {
                NetPager pager = new NetPager();
                pager.TableName = "Bulletin";
                pager.PageIndex = pageindex;
                pager.PrimaryKey = "BulletinID";
                pager.Where = string.Format("(UserID='{0}' or UserID='') and IsDel={1}", userid, (int)Common.Enum.auvDel.NotDel);
                pager.Order = "CreateDate desc,BulletinID asc";
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.PageSize = pagesize;
                IList<Bulletin> result = dao.GetPagedList(pager);
                recordcount = pager.TotalCount;

                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }


        #endregion

        #region 获取公告的链接地址

        /// <summary>
        /// 获取公告的链接地址
        /// </summary>
        /// <param name="bulletin"></param>
        /// <returns></returns>
        public string GetRedirectPath(Bulletin bulletin)
        {
            if (bulletin.RedirectID.Contains("Pages"))
            {
                //兼容原有数据
                return bulletin.RedirectID;
            }
            //发送给买家的订单公告
            if (bulletin.StatusID == (int)Common.Enum.bulletinStatus.BeBuyer)
            {
                return "/Pages/ShoppingDetail.aspx?id=" + bulletin.RedirectID;
            }
            else if (bulletin.StatusID == (int)Common.Enum.bulletinStatus.BeSeller)
            {
                return "/Pages/OrderDetail.aspx?id=" + bulletin.RedirectID;
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region 将订单相关的公告设为已读

        /// <summary>
        /// 将订单相关的公告设为已读
        /// </summary>
        public void ReadBulletinByOrderID(string orderID,Common.Enum.bulletinStatus status)
        {
            try
            {
                Hashtable ht = new Hashtable();

                ht.Add("redirectID", string.Format("%{0}%", orderID));
                ht.Add("statusID", (int)status);
                ht.Add("isRead", false);
                ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);

                IList<Bulletin> list = dao.GetListByParams(ht);
                foreach (var item in list)
                {
                    item.IsRead = true;
                    Update(item);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        #endregion

        #endregion
    }
}
